<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use think\helper\Hash;
use think\Db;

/**
 * 客户模型
 */
class Client extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_client';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //重要程度
    public static $point = [
        0       =>  '低',
        1       =>  '中',
        2       =>  '高',
    ];
    public static $point_tag_color = [
        0       =>  '',
        1       =>  'badge-primary',
        2       =>  'badge-danger',
    ];

    //客户类型
    public static $types = [
        0       =>  '自有',
        1       =>  '外协',
    ];

    //员工类型
    public static $staff_type = [
        1       =>  '业务经理',
        2       =>  '内勤',
    ];

    //ajax客户列表配置
    public static $ajaxTableField = [
        'nickname'      => '客户联系人',
        'mobile'        => '客户联系电话',
        'company'       => '客户公司',
        'address'       => '客户地址',
        'sales'         => '销售经理',
        'sales_mobile'  => '销售经理电话',
        'back'          => '跟单人员',
        'note'          => '备注',
    ];

    //获取所有客户
    public static function getList($field=''){
        if(empty($field))$field = "concat(nickname,'--',mobile) as str_name";
        $list = Db::name('mill_client')->where('status=1')->column($field,'id');
        return $list;
    }

    //获取员工
    public static function getStaffList($type = null,$field = 'staff'){
        $where = [['status','=',1]];
        if(is_numeric($type))$where[] = ['type','in',$type];
        $list = Db::name('mill_staff')->where($where)->order('id desc')->column($field,'id');
        return $list;
    }

    //获取所有部门
    public static function getGroupList(){
        $list = Db::name('admin_user_group')->where('status=1')->column("name",'id');
        return $list;
    }

    //获取生产车间
    public static function getMillList(){
        $list = Db::name('admin_user_mill')->where('status=1')->column("name",'id');
        return $list;
    }

    //获取操作员
    public static function getUserList($type = ''){

        if($type == 'all'){
            $list = Db::name('admin_user')->column("nickname",'id');
        }
        else{
            $map[] = ['g.give_sort','>',0];
            $map[] = ['u.status','=',1];
            $map[] = ['g.status','=',1];
            $list = Db::name('admin_user')
                ->alias('u')
                ->leftJoin('admin_user_group g','u.group=g.id')
                ->where($map)
                ->order('g.give_sort asc,u.id desc')
                ->column("concat(g.name,'[',u.nickname,']') as str_name",'u.id');
        }

        return $list;
    }

    //根据客户信息设置标签
    public static function getTags($data){

        $point = self::$point[$data['point']];
        $point_color = self::$point_tag_color[$data['point']];
        $new = $data['create_time']>=strtotime(date('Y-m-d'))?'<span class="label st badge-success">新</span>':'';
        $is_lock = $data['is_lock']==0?'<span class="label st badge-warning">锁</span>':'';
        $tags = '<span class="label st '.$point_color.'">'.$point.'</span>'.$new.$is_lock;

        return $tags;
    }

    // 表格制作
    //print 0:编辑模式 1:打印模式
    /**
        'field'     =>  'ids',//字段名
        'title'     =>  '',//标题
        'type'      =>  'checkbox',//normal（常规列，无需设定） checkbox（复选框列） radio（单选框列） numbers（序号列） space（空列）
        'colspan'   =>  '1',//单元格所占列数
        'rowspan'   =>  '1',//单元格所占行数
        'width'     =>  '28px',//宽度
        'minWidth'  =>  '28px',//最小宽度
        'templet'   =>  '',//自定义列模板
        'fixed'     =>  true,//固定列。可选值有：left（固定在左）、right（固定在右）
        'escape'    =>  true,//当前列是否开启编码（转义 html）
        'totalRow'  =>  true,//是否开启该列的自动合计，详细查看：https://layui.dev/2.7/docs/modules/table.html#cols
     */
    public static function fromSelect($data = [],$print = 0){

        $layui_head = [
            [
                'field'     =>  'ids',
                //'type'      =>  'checkbox',
                'title'     =>  '<div class="layui-form check-all"><input type="checkbox" lay-skin="primary"></div>',
                'width'     =>  '28',
                'escape'    =>  false,
            ],
            [
                'field'     =>  'company',
                'title'     =>  '客户名称',
                'align'     =>  'center',
            ],
            [
                'field'     =>  'sales',
                'title'     =>  '原业务员',
                'align'     =>  'center',
            ],
            [
                'field'     =>  'order_sum',
                'title'     =>  '工程数量',
                'align'     =>  'center',
            ],
            [
                'field'     =>  'batch_sum',
                'title'     =>  '批次数量',
                'align'     =>  'center',
            ],
            [
                'field'     =>  'area_sum',
                'title'     =>  '总面积',
                'align'     =>  'center',
            ],
        ];

        $table_head = $table_data = [];
        foreach ($layui_head as $lh){
            $table_head[] = $lh['title'];
        }
        $table_head = [$table_head];

        if(!empty($data['list'])){
            foreach ($data['list'] as $tdv){
                $tdv_data = [];

                foreach ($layui_head as $lh){

                    if($lh['field'] == 'ids')$tdv_data[] = '
<div class="layui-form check-one"><input type="checkbox" name="ids[]" value="'.$tdv['id'].'" lay-skin="primary"><div class="layui-form-checkbox" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div></div>';
                    else $tdv_data[] = isset($tdv[$lh['field']])?$tdv[$lh['field']]:'';
                }

                $table_data[] = $tdv_data;
            }
        }

        return ['head'=>$table_head,'data'=>$table_data,'layui_head'=>$layui_head];
    }

}
